var changePay = {};
changePay.methods = {};

$(function(){
    // 手机号四位隐藏
    changePay.methods.phoneTxtHide();

    // 验证身份表单校验
    changePay.methods.identityFormCheck();

    // 验证身份表单提交
    changePay.methods.identityFormSubmit();

    // 支付密码表单校验
    changePay.methods.payFormCheck();

    // 支付密码表单提交
    changePay.methods.payFormSubmit();

    // 验证身份验证码
    changePay.methods.codeBtn();

})

// 手机号四位隐藏
changePay.methods.phoneTxtHide = function(){
    var phoneTxtFirst = $(".shop-identity-form .shop-pay-txt").text().substring(0,3);
    var phoneTxtLast = $(".shop-identity-form .shop-pay-txt").text().substring(7,11);
    $(".shop-identity-form .shop-pay-txt").text(phoneTxtFirst + "****" + phoneTxtLast);
}

// 验证身份表单校验
changePay.methods.identityFormCheck = function(){
    //验证规则
    $(".shop-identity-form").validate({

        rules:{
            codeNumber:{
                required:true,
                minlength:4,
                maxlength:4
            }
        },
        messages:{
            codeNumber:{
                required:"请输入短信验证码",
                minlength:"验证码为四位",
                maxlength:"验证码为四位"
            }
        }
    });
}

// 验证身份表单提交
changePay.methods.identityFormSubmit = function(){
    $(".shop-identity-form").validate({
        submitHandler:function(form){
            $(form).ajaxSubmit();
        }
    })  

    $(".shop-identity-form .shop-identity-btn").on("click",function(){
        if($(".shop-identity-form").valid()){
            $(".shop-account-pay-form").hide();
            $(".shop-pay-form").show();
            $(".shop-account-pay-title .shop-pay").addClass("shop-sel");
        }
    })
}

// 支付密码表单校验
changePay.methods.payFormCheck = function(){
    //验证规则
    $(".shop-pay-form").validate({
        rules:{
            payNumber:{
                required:true,
                number:true,
                minlength:6,
                maxlength:6,
            },
            repeatPayNumber:{
                required:true,
                number:true,
                equalTo: "#payNumber"
            }
        },
        messages:{
            payNumber:{
                required:"请输入支付密码",
                number:"请正确填写支付密码" ,
                minlength:"请输入六位支付密码",
                maxlength:"请输入六位支付密码",
            },
            repeatPayNumber:{
                required:"请再次入支付密码",
                number:"请正确填写支付密码" ,
                equalTo: "两次密码不一致"
            }
        }
    });
}
// 支付密码表单提交
changePay.methods.payFormSubmit = function(){
    $(".shop-pay-form").validate({
        submitHandler:function(form){
            $(form).ajaxSubmit();
        }
    })  

    $(".shop-pay-form .shop-pay-btn").on("click",function(){
        if($(".shop-pay-form").valid()){
            $(".shop-account-pay-form").hide();
            $(".shop-account-complete").show();
            $(".shop-account-pay-title .shop-complete").addClass("shop-sel");
        }
    })
}



// 获取验证码公用
changePay.methods.codeBtn = function(){
    var flag = true;
    $(".shop-identity-form .shop-code-btn").on("click", function () {
        // 启动定时器
        var alltime = 60 * 1000;
        if(flag){
            $(this).html("60s");
            flag = false;
            var timer = setInterval(function () {
                alltime = alltime - 1000;
                var str = parseInt(alltime / 1000) + "s";
                $(".shop-identity-form .shop-code-btn").html(str.toString());
                if (alltime <= 0) {
                    clearInterval(timer);
                    $(".shop-identity-form .shop-code-btn").html("获取验证码");
                    flag = true;
                }
            }, 1000)
        }
        
    });
}

